package com.gct.config;
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
//@EnableWebMvc
@PropertySource({ "classpath:com/gct/config/datasource.properties" })
public class DataSourceConfig {

    @Autowired
    private Environment env;

    @Bean(name="dataSource")
    @Profile("dev")
    public DataSource devdataSource() throws Exception {
//        Context ctx = new InitialContext();
//        return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
        BasicDataSource dataSource =  new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("gct.db.driverClassName"));
        dataSource.setUrl(env.getProperty("gct.db.url"));
        dataSource.setUsername(env.getProperty("gct.db.username"));
        dataSource.setPassword(env.getProperty("gct.db.password"));
        dataSource.setMaxWait(Integer.parseInt(env.getProperty("gct.db.maxWait")));
        dataSource.setMinIdle(Integer.parseInt(env.getProperty("gct.db.minIdle")));
        dataSource.setMaxIdle(Integer.parseInt(env.getProperty("gct.db.maxIdle")));
        dataSource.setMaxActive(Integer.parseInt(env.getProperty("gct.db.maxActive")));
        return dataSource;
    }

/*    @Bean
    @Profile("javaee")
    public JndiObjectFactoryBean dataSource() throws IllegalArgumentException {
        JndiObjectFactoryBean dataSource = new JndiObjectFactoryBean();
        dataSource.setExpectedType(DataSource.class);
        dataSource.setJndiName(env.getProperty("jdbc.jndiDataSource"));
        return dataSource;
    }

    @Bean
    @Profile("test")
    public DataSource testDataSource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
    }*/
}
